package com.bhs.sansong.ui.main;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager2.widget.ViewPager2;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.FragmentAdapter;
import com.bhs.sansong.base.BaseBean;
import com.bhs.sansong.base.BaseFragment;
import com.bhs.sansong.bean.ClearMessage;
import com.bhs.sansong.bean.MessageCenterRefresh;
import com.bhs.sansong.bean.NewMessage;
import com.bhs.sansong.bean.ViewPageStatus;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.ui.message.CircleFriendsFragment;
import com.bhs.sansong.ui.message.CircleFriendsMessageActivity;
import com.bhs.sansong.ui.message.MessageCenterFragment;
import com.bhs.sansong.utils.SharedPreferencesUtils;
import com.bhs.sansong.utils.ToastUtil;
import com.bhs.sansong.view.SelectedFont3;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import okhttp3.ResponseBody;
import retrofit2.Response;

public class MessageFragment extends BaseFragment implements View.OnClickListener, NetCallBack {

    private TabLayout mTabLayout;

    private ViewPager2 mViewPage;
    private TextView mTvClearUnread;
    private FrameLayout mFlInform;
    private View mMessageHint;
    private View mCircleHint;
    private int mPosition;
    public static MessageFragment newInstance() {
        return new MessageFragment();
    }


    @Override
    public int initLayout() {
        return R.layout.fragment_message;
    }

    @Override
    public void initView(View root) {
        mTabLayout = root.findViewById(R.id.tabLayout);
        mViewPage = root.findViewById(R.id.view_page);
        mTabLayout.setTabMode(TabLayout.MODE_AUTO);
        mTabLayout.addOnTabSelectedListener(new SelectedFont3(activity));
        mTvClearUnread = root.findViewById(R.id.tv_clear_unread);
        mFlInform = root.findViewById(R.id.fl_inform);
        mFlInform.setOnClickListener(this);
        mTvClearUnread.setOnClickListener(this);
        mMessageHint = root.findViewById(R.id.message_hint);
        mCircleHint = root.findViewById(R.id.circle_hint);
        EventBus.getDefault().register(this);
    }


    @Override
    public void onResume() {
        super.onResume();
        EventBus.getDefault().post(new MessageCenterRefresh());
    }
    @Override
    public void initData() {
        ArrayList<BaseFragment> fragments = new ArrayList<>();
        fragments.add(MessageCenterFragment.newInstance());
        String name = SharedPreferencesUtils.getString(getContext(), "niceName");
        String face = SharedPreferencesUtils.getString(getContext(), "face");
        String BgCover = SharedPreferencesUtils.getString(getContext(), "circle_pic");
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        bundle.putString("face", face);
        bundle.putString("BgCover", BgCover);
        bundle.putInt("type", 1);
        fragments.add(CircleFriendsFragment.newInstance(bundle));

        mViewPage.setOffscreenPageLimit(fragments.size());
        mViewPage.setAdapter(new FragmentAdapter(activity, fragments));
        new TabLayoutMediator(mTabLayout, mViewPage, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(position == 0 ? "消息" : "圈子");
            }
        }).attach();

        mViewPage.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mPosition = position;
                if (position == 1) {
                    mMessageHint.setVisibility(View.GONE);
                    mFlInform.setVisibility(View.VISIBLE);
                    mTvClearUnread.setVisibility(View.GONE);
                } else {
                    mTvClearUnread.setVisibility(View.VISIBLE);
                    mFlInform.setVisibility(View.GONE);
                }
            }
        });
    }

    @Subscribe
    public void onEvent(ViewPageStatus event) {
        mViewPage.setUserInputEnabled(event.isEnabled());
    }

    @Subscribe
    public void onEvent(ClearMessage event) {
        mCircleHint.setVisibility(View.GONE);
        mMessageHint.setVisibility(View.GONE);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.fl_inform) {
            startActivity(new Intent(getContext(), CircleFriendsMessageActivity.class));
            EventBus.getDefault().post(new ClearMessage());
        } else if (view.getId() == R.id.tv_clear_unread) {
            loadingPopupShow();
            networkRequest(netApi.lots_read_message("cargo"), this);
        }
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        BaseBean bean = new Gson().fromJson(json, BaseBean.class);
        loadingPopupDismiss();
        if (bean.isStatus()) {
            EventBus.getDefault().post(new MessageCenterRefresh());
        } else {
            ToastUtil.show(bean.getMsg());
        }
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEvent(NewMessage event) {
        if (event.getType().equals("quanzi")) {
            if (mPosition != 1) {
                mMessageHint.setVisibility(View.VISIBLE);
            }
            if (TextUtils.isEmpty(event.getPage()) || !event.getPage().equals("666")) {
                mCircleHint.setVisibility(View.VISIBLE);
            }
        }


    }

    @Override
    public void onFailure(int requestCode) {

    }
}